package org.se.scut.pinterest.mina.codec.filte.coder.encoder;

import java.util.List;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.filter.codec.demux.MessageEncoder;
import org.se.scut.pinterest.mina.codec.msg.PicturesPostMessage;
import org.se.scut.pinterest.mina.codec.msg.PicturesPostMessageItem;

public class PicturesPostEncoder extends BaseMessageEncoder 
	implements MessageEncoder<PicturesPostMessage> {

	@Override
	public void encode(IoSession session, PicturesPostMessage msg,
			ProtocolEncoderOutput out) throws Exception {
		IoBuffer buffer = IoBuffer.allocate(2048).setAutoExpand(true);
		buffer.putString(msg.getSymbol() + delimiter, ENCODER);
		buffer.putString(msg.getUserID() + delimiter, ENCODER);
		buffer.putString(msg.getAblumID() + delimiter, ENCODER);
		buffer.putString(msg.getPictureTypeID() + delimiter, ENCODER);
		buffer.putString(msg.getFrom() + delimiter, ENCODER);
		buffer.putString(msg.getCount() + delimiter, ENCODER);
		buffer.putString(msg.getItems().size() + delimiter, ENCODER);
		List<PicturesPostMessageItem> items = msg.getItems();
		int size = items.size();
		for (int i = 0; i < size; i++) {
			buffer.putString(items.get(i).getId() + delimiter, ENCODER);
			buffer.putString(items.get(i).getFileName() + delimiter, ENCODER);
			buffer.putString(items.get(i).getOverView() + delimiter, ENCODER);
			buffer.putString(items.get(i).getNumOfCollected() + delimiter, ENCODER);
			buffer.putString(items.get(i).getCreateTime() + delimiter, ENCODER);
			buffer.putString(items.get(i).getPictureTypeID() + delimiter, ENCODER);
			buffer.putString(items.get(i).getFileLength() + delimiter, ENCODER);
			buffer.put(items.get(i).getContent());
		}
		buffer.flip();
		out.write(buffer);
		
	}

}
